package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kyleduo.switchbutton.SwitchButton;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.bi.AddAssetsActivity;
import com.stn.zjx.givingtreasure.bean.EthItem;
import com.stn.zjx.givingtreasure.domain.TokenListDomain;
import com.stn.zjx.givingtreasure.event.BiMainRefreshEvent;
import com.stn.zjx.givingtreasure.event.RefreshAddAssetsEvent;
import com.stn.zjx.givingtreasure.net.impl.EthNetImpl;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class TokenSearchItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<EthItem> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public AddAssetsActivity context;

   public TokenSearchItemListAdapter(AddAssetsActivity context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_token_search_item, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       EthItem dataBean=dataBeanList.get(i);
       Glide.with(context)
               .asBitmap()
               .load(dataBean.getEthTokenIcon())
               .into(ih.icon_b);
       ih.symbol.setText(dataBean.getEthSymbolName());
       ih.name.setText(dataBean.getEthTokenName());
       if(TextUtils.isEmpty(dataBean.getEthTokenAddress())){
           ih.address.setText("");
       }else{
           ih.address.setText(
                   dataBean.getEthTokenAddress().substring(0,10)+
                           "..."
                   +dataBean.getEthTokenAddress().substring(dataBean.getEthTokenAddress().length()-10,dataBean.getEthTokenAddress().length())
           );
       }
      switch (dataBean.getEthTokenShow()){
          case 1:
            ih.switch_bt.setVisibility(View.GONE);
              break;
          default:
              ih.switch_bt.setVisibility(View.VISIBLE);
              break;
      }
       ih.switch_bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

           }
       });
        switch (dataBean.getIsOpen()){
            case 0:
                ih.switch_bt.setChecked(false);
                break;
            case 1:
                ih.switch_bt.setChecked(true);
                break;
        }
        ih.switch_bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                  if(b){
                      dataBean.setIsOpen(1);
                      addToken(context.address,dataBean.getEthTokenId());
                  }else{
                      dataBean.setIsOpen(0);
                      deleteToken(context.address,dataBean.getEthTokenId());
                  }

            }
        });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<EthItem> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<EthItem> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
             public ImageView icon_b;
           public TextView symbol;
       public TextView name;
         public TextView address;
         public SwitchButton switch_bt;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            icon_b=itemView.findViewById(R.id.icon_b);
            symbol=itemView.findViewById(R.id.symbol);
            name=itemView.findViewById(R.id.name);
            address=itemView.findViewById(R.id.address);
            switch_bt=itemView.findViewById(R.id.switch_bt);
        }
   }

   public void addToken(String ethAddress,Integer ethTokenId){
       EthNetImpl
               .eth_token_add_show(ethAddress, ethTokenId, ethAddress+"_eth_token_add_show_"+ethTokenId, new StringCallback() {
                   @Override
                   public void onSuccess(Response<String> response) {
                       RefreshAddAssetsEvent event=new RefreshAddAssetsEvent();
                       EventBus.getDefault().post(event);
                   }
               });
   }
    public void deleteToken(String ethAddress,Integer ethTokenId){
        EthNetImpl
                .eth_token_delete_show(ethAddress, ethTokenId, ethAddress+"_eth_token_delete_show_"+ethTokenId, new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        RefreshAddAssetsEvent event=new RefreshAddAssetsEvent();
                        EventBus.getDefault().post(event);
                    }
                });
    }

}